import cgi
import datetime
import urllib
import webapp2

import jinja2
import os

import json

dir = os.path.dirname(__file__)
filename = os.path.join(dir, '../view')
jinja_environment = jinja2.Environment(loader=jinja2.FileSystemLoader(filename))

from google.appengine.ext import db

from model.Person import Person
from model.User import User

class AddGroup(webapp2.RequestHandler):

    def get(self):
        curr_fb_uid = int(self.request.get('fb_uid'))
        current_user = User.all().filter('fb_uid =', curr_fb_uid).fetch(1)
        
        if current_user:
            template = jinja_environment.get_template('menu.html')
            self.response.out.write(template.render())
        else:
            template = jinja_environment.get_template('mini-registration.html')
            self.response.out.write(template.render())

    def post(self):
        #self.response.out.write("Hola")
        #supongo que ya esta guardado en Users
        fb_uid = int(self.request.get('fb_uid'))
        
        user = User.all().filter('fb_uid =', fb_uid).fetch(1)
        if user:
            seeking = user.searching()

        p1 = Person(fb_uid=fb_uid, gender='male', searching=seeking)
        p1.put()

        self.redirect('/list')